package com.itheima.dao;

import com.itheima.controller.BaseStudentController;
import com.itheima.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    private static ArrayList<Student> studentArrayList = new ArrayList<>();
    static {
        System.out.println("----------------系统启动中,数据开始初始化,请稍后!----------------");
        studentArrayList.add(new Student("001","张三","25","1999-11-11"));
        studentArrayList.add(new Student("002","李四","24","1993-01-11"));
        studentArrayList.add(new Student("003","王五","22","1996-12-11"));
        studentArrayList.add(new Student("004","赵六","26","1997-06-11"));
        System.out.println("----------------系统启动完毕,数据初始化成功,进入系统!----------------");
    }

    //添加学生的方法
    public boolean addStudent(Student student) {
        studentArrayList.add(student);
        return true;
    }

    //删除学生的方法
    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        studentArrayList.remove(index);
    }

    //修改学生的方法
    public void updateStudent(String sid, Student student) {
        int index = getIndex(sid);
        studentArrayList.set(index,student);
    }

    //查看学生的方法
    public Student[] showStudent() {
        Student[] students = new Student[studentArrayList.size()];
        for (int i = 0; i < studentArrayList.size(); i++) {
            students[i] = studentArrayList.get(i);
        }
        return students;
    }

    //给Service返回数组的方法
    public Student[] findAllStudent() {
        Student[] students = new Student[studentArrayList.size()];
        for (int i = 0; i < studentArrayList.size(); i++) {
            students[i] = studentArrayList.get(i);
        }
        return students;
    }

    //根据学号查找索引的方法
    public int getIndex(String sid) {
        int index = -1;
        for (int i = 0; i < studentArrayList.size(); i++) {
            if (studentArrayList.get(i).getSid().equals(sid)){
                index = i;
            }
        }
        return index;
    }

}
